/*
 * Copyright 2008 Jens Meiss
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package mtt.rms.recordstores.options;

import java.io.IOException;

import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;

import mtt.rms.dataObjects.ApplicationOptions;
import mtt.rms.recordstores.options.events.GetApplicationOptionsEvent;
import mtt.rms.recordstores.options.events.SetApplicationOptionsEvent;
import mtt.rms.recordstores.options.events.UpdateApplicationOptionsEvent;

import hmvc.j2me.component.IComponent;
import hmvc.j2me.events.IEvent;
import hmvc.j2me.framework.rms.ObjectStore;

/**
 * 
 * 
 * @author jens.meiss
 */
public class RecordStoreOptions extends ObjectStore {

	/**
	 * specify the name of the "Marks"-RecordStore name
	 */
	private static String name = "MTT_Optionen";

	private void getApplicationOptions(IComponent component)
			throws RecordStoreNotOpenException, RecordStoreException,
			IOException {

		ApplicationOptions applicationOptions = new ApplicationOptions();

		if (getNumRecords() == 0) {
			/*
			 * noch nicht vorhandenen Eintrag anlegen
			 */
			store(applicationOptions);

		} else {
			RecordEnumeration enumeration = enumerateRecords(null, null, false);
			deserializeRecord(enumeration.nextRecord(), applicationOptions);
		}

		component.fireEventLocal(new SetApplicationOptionsEvent(
				applicationOptions));

	}

	protected String getRecordStoreName() {
		return name;
	}

	public void handleEvent(IEvent event) {
		try {
			if (event instanceof GetApplicationOptionsEvent) {

				GetApplicationOptionsEvent applicationOptionsEvent = (GetApplicationOptionsEvent) event;
				getApplicationOptions(applicationOptionsEvent.getComponent());

			} else if (event instanceof UpdateApplicationOptionsEvent) {

				UpdateApplicationOptionsEvent updateApplicationOptionsEvent = (UpdateApplicationOptionsEvent) event;
				updateApplicationOptions(updateApplicationOptionsEvent
						.getApplicationOptions());
			}
		} catch (RecordStoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void init() {
		open();
	}

	public void registerEvents() {
		register(GetApplicationOptionsEvent.class, this);
		register(UpdateApplicationOptionsEvent.class, this);
	}

	public void terminate() {
		close();
	}

	private void updateApplicationOptions(ApplicationOptions applicationOptions)
			throws RecordStoreException, IOException {

		update(applicationOptions);

	}

}
